﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Metadata.Entity;

namespace T.FAS.Runtime.Metadata.Entity.DataModel
{
    /// <summary>
    /// 规则定义
    /// </summary>
    public class TRule : ICloneable, IRefSchemaDetail
    {
        /// <summary>
        /// 主键id
        /// </summary>
        public string ID { get; set; }

        /// <summary>
        /// 所属状态机ID
        /// </summary>
        [JsonIgnore]
        public string StateMachineID { get; set; }

        /// <summary>
        /// 所属状态ID
        /// </summary>
        [JsonIgnore]
        public string StateID { get; set; }

        /// <summary>
        /// 规则标识
        /// </summary>
        public string Key { get; set; }

        /// <summary>
        /// 规则名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 规则描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 是否在界面状态转换时执行规则的预处理
        /// </summary>
        public bool IsPreCheck { get; set; }

        /// <summary>
        /// false:无前置条件，true:有前置条件
        /// </summary>
        public bool HasCondition { get; set; }

        /// <summary>
        /// 前置条件表达式内容
        /// </summary>
        public string ConditionExpression { get; set; }

        /// <summary>
        /// 前置条件表达式内容Json内容，供表达式SDK使用
        /// </summary>
        public string ConditionExpressionJson { get; set; }

        public List<TRuleField> RuleFields { get; set; }

        public List<TRuleContent> RuleContents { get; set; }

        ///// <summary>
        ///// RuleType:Expression时，存储条件表达式参数Json内容
        ///// RuleType:System时，存储需要参数的系统条件的参数Json内容
        ///// </summary>
        //public string ParameterString { get; set; }

        /// <summary>
        /// 排序
        /// </summary>
        public int Sort { get; set; }

        //private Dictionary<string, object> paramters;

        ///// <summary>
        ///// Rule参数信息
        ///// </summary>
        //public Dictionary<string, object> Paramters
        //{
        //    get
        //    {
        //        //获取参数时，反序列化ParameterString中的参数Json
        //        if (paramters == null && !string.IsNullOrEmpty(ParameterString))
        //        {
        //            try
        //            {
        //                paramters = JsonConvert.DeserializeObject<Dictionary<string, object>>(ParameterString);
        //            }
        //            catch (Exception e)
        //            {
        //                throw new FasException(DataModelErrorCode.SM.SerializeError, $"状态机:{StateMachineID}的规则:{this.Key}入参Json:{ParameterString}反序列化失败，请检查", e);
        //            }
        //        }
        //        return paramters;
        //    }
        //    set
        //    {
        //        if (value != null)
        //        {
        //            paramters = value;
        //            ParameterString = JsonConvert.SerializeObject(value);
        //        }
        //    }
        //}

        /// <summary>
        /// 备用字段
        /// </summary>
        [JsonIgnore]
        public string Ext1 { get; set; }

        /// <summary>
        /// 备用字段
        /// </summary>
        [JsonIgnore]
        public string Ext2 { get; set; }

        /// <summary>
        /// 备用字段
        /// </summary>
        [JsonIgnore]
        public string Ext3 { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        [JsonIgnore]
        public string Creator { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>      
        [JsonIgnore]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 最后修改人
        /// </summary>
        [JsonIgnore]
        public string LastModifier { get; set; }

        /// <summary>
        /// 最后修改时间
        /// </summary>      
        [JsonIgnore]
        public DateTime LastModifyTime { get; set; }

        public object Clone()
        {
            return this.MemberwiseClone();
        }

        public string GetRefSchemaExpr(IMetaDataContent metaData)
        {
            if (metaData is StateMachineMetadata sm)
            {
                try
                {
                    string prefix = sm.GetTopRefSchemaExpr();
                    string stateConst = nameof(StateMachineMetadata.States);
                    string ruleConst = nameof(TState.Rules);
                    TState state;
                    if (string.IsNullOrEmpty(this.StateID))
                    {
                        state = sm.States.FirstOrDefault(s => s.Rules.Exists(r => r.ID == this.ID));
                    }
                    else
                    {
                        state = sm.States.FirstOrDefault(s => s.ID == this.StateID);
                    }
                    string statePrefix = $"{prefix}.{stateConst}.__{state.Key}";
                    var result = $"{statePrefix}.{ruleConst}.{this.ID.Replace("-", "")}.{RefSchemaExprUtil.Const.Text}";
                    return result;
                }
                catch (Exception e)
                {
                }
            }
            return null;
        }
    }

    public enum RuleType
    {
        System,
        Expression
    }
}
